<template>
    <div style="width: 100%">
        <div id="deawer">
            <v-tableTitle :btnConfig="btnConfig" @cancel="cancel" @save="save"></v-tableTitle>
            <div class="detailBox">
                <v-detail ref="detail"></v-detail>
            </div>
            <div class="detailListBox" v-if="flag">
                <v-detailList ref="detailList"></v-detailList>
            </div>
        </div>
    </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import skipMixin from "@/mixins/SkipMixin";
import tableTitle from "commons/TableTitle";
import detail from "../details/Detail";
import detailList from "../detailsList/DetailList";
import config from "../Config";


export default {
    name: "",
    mixins: [baseMixin, skipMixin, config],
    data() {
        return {
            flag: true,
        };
    },
    computed: {
        btnConfig() {
            return [
                { label: this.$t('common.save'), certigier: this.saveStatus||!this.btnPermNameList.includes('WM010302'), invalid: "save" },
                { label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM010303'),  invalid: "cancel" },
            ]
        }
    },
    created() {
    },
    methods: {
        //保存按钮
        save() {
            this.$refs.detail.$children[0].$refs.detaRef.$refs["from"].validate(async (valid) => {
                if (valid) {
                    let detail = this.$refs.detail.detailData;
                    if (this.flag) {
                        if (this.$refs.detailList.selectData.length == 0) {
                            this.$message.error(this.$t('msg.msg53'));
                            return;
                        } else {
                            detail.adjustmentdetails = this.$refs.detailList.selectData;
                        }
                    } else {
                        if (this.$refs.detail.temporary.length == 0) {
                            this.$message.error(this.$t('msg.msg53'));
                            return;
                        }
                        detail.adjustmentdetails = this.$refs.detail.temporary;
                    }
                    detail.whseid = this.$store.state.wms;
                    try {
                        let res = await this.$api.post('/excuteReceiptreversal', detail);
                        
                            this.$message.success(res.returnMgr);
                            this.cancel();
                        
                    } catch (e) {
                        this.$message.error(e)
                    }
                }
            })
        },
        //弹框取消
        cancel() {
            this.$router.push({ name: "receiptreversal" });
        },
    },
    mounted() {
        if (Object.keys(this.$store.state.skip).length !== 0) {
            let data = this.$store.state.skip;
            if (data.newFlag) {
                this.flag = false
            }
        } else if (this.$store.state.newFlag) {
            this.flag = false
        }
    },
    watch: {},
    components: {
        "v-tableTitle": tableTitle,
        "v-detail": detail,
        "v-detailList": detailList,
    }
}
</script>

<style scoped>
</style>
